﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace The_LarX
{
    public class LarTrackBar : IUpdateable
    {
        public Rectangle TrackRectangle { get; protected set; }
        public Rectangle SliderRectangle;

        public Camera2D Camera;

        public event EventHandler OnSlide;

        public float Maximum;
        public float Minimum;

        private float currentValue;
        public float CurrentValue
        {
            get { return currentValue; }
            set
            {
                currentValue = (int)MathHelper.Clamp(value, Minimum, Maximum);
                if (UsingX)
                {
                    float End = Maximum - Minimum;
                    float val = value / End;
                    float Dif = MaxX - MinX;
                    SliderRectangle.X = (int)MathHelper.Clamp((int)(MinX + (Dif * val)),
                        MinX, MaxX);
                }
                else
                {
                    float End = Maximum - Minimum;
                    float val = value / End;
                    float Dif = MaxY - MinY;
                    SliderRectangle.Y = (int)MathHelper.Clamp((int)(MinY + (Dif * val)),
                        MinY, MaxY);
                }
            }
        }

        public LarTrackBar(int Width, int Height, int X, int Y,
            int SliderWidth, int SliderHeight, float Minimum,
            float Maximum)
        {
            TrackRectangle = new Rectangle(X, Y, Width, Height);
            SliderRectangle = new Rectangle(X - (SliderWidth / 2) + (Width / 2),
                Y - (SliderHeight / 2) + (Height / 2), SliderWidth, SliderHeight);

            UsingX = TrackRectangle.Width > TrackRectangle.Height;

            this.Minimum = Minimum;
            this.Maximum = Maximum;
        }

        public bool UsingX;

        public float Porcentage
        {
            get
            {
                if (UsingX)
                {
                    float End = MaxX - MinX;
                    float fin = 100 / End;
                    return ((float)CurrentSliderPosition * fin);
                }
                else
                {
                    float End = MaxY - MinY;
                    float fin = 100 / End;
                    return ((float)CurrentSliderPosition * fin);
                }
            }
        }
        public int CurrentSliderPosition
        {
            get 
            {
                if (UsingX)
                {
                    return SliderRectangle.X - TrackRectangle.X;
                }
                else
                {
                    return SliderRectangle.Y - TrackRectangle.Y;
                }
            }
        }
        public int MinX
        {
            get { return TrackRectangle.X; }
        }
        public int MaxX
        {
            get { return TrackRectangle.X + TrackRectangle.Width - SliderRectangle.Width; }
        }
        public int MinY
        {
            get { return TrackRectangle.Y; }
        }
        public int MaxY
        {
            get { return TrackRectangle.Y + TrackRectangle.Height - SliderRectangle.Height; }
        }

        private bool moving = false;
        public virtual void Update()
        {
            float hundred = Porcentage;
            Rectangle mouseRect = Engine.Input.MouseRect(1, Camera);
            if (mouseRect.Intersects(SliderRectangle) &&
                Engine.Input.MouseLeftClick())
            {
                moving = true;
            }
            if (Engine.Input.MouseLeftUp())
            {
                moving = false;
            }

            if (moving)
            {
                if (Minimum < Maximum)
                {
                    if (UsingX)
                    {
                        float X = Engine.Input.MouseState.X;
                        if (Camera != null)
                            X += Camera.Position.X;
                        X = MathHelper.Clamp(X, MinX, MaxX);
                        SliderRectangle.X = (int)X;

                        float Size = MaxX - MinX;
                        float Factor = Maximum / Size;
                        currentValue = (X - MinX) * Factor;

                        if (OnSlide != null)
                        {
                            OnSlide(this, EventArgs.Empty);
                        }

                    }
                    else
                    {
                        float Y = Engine.Input.MouseState.Y;
                        if (Camera != null)
                            Y += Camera.Position.Y;
                        Y = MathHelper.Clamp(Y, MinY, MaxY);
                        SliderRectangle.Y = (int)Y;

                        float Size = MaxY - MinY;
                        float Factor = Maximum / Size;
                        currentValue = (Y - MinY) * Factor;

                        if (OnSlide != null)
                        {
                            OnSlide(this, EventArgs.Empty);
                        }
                    }
                }
            }
        }

        public virtual void Draw()
        {
            Engine.RenderManager.DrawRectangle(Color.Black, TrackRectangle, Camera, 2, 200);
            Engine.RenderManager.DrawRectangle(Color.White, SliderRectangle, Camera, 2, 200);
        }
    }
}
